package com.becca.cake.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.becca.cake.dao.system.DeptDAO;
import com.becca.cake.model.system.DeptDO;
import com.becca.cake.model.system.MenuTree;
import com.becca.cake.model.system.Node;
import com.becca.cake.service.system.IUserDeptService;
import com.becca.cake.util.JsonUtil;

/**
 * 部门管理Service实现
 * @author hejinguo
 * @version $Id: UserDeptServiceImpl.java, v 0.1 2013-12-2 上午12:50:06
 */
@Service
public class UserDeptServiceImpl implements IUserDeptService {
    @Autowired
    private DeptDAO deptDAO;

    /**
     * 设置缓存
     */
    @Override
    @Cacheable(key = "'selectDeptMsgById'+#depeId", value = "monmondecakeCache")
    public String selectDeptTreeById(Long depeId) throws Exception {
        //查询该部门下的所有部门
        List<Node> depts = this.deptDAO.selectDeptTreeById(depeId);
        //根据ID查询当前功能
        Node node = this.deptDAO.selectDeptById(depeId);
        //封装菜单树json
        MenuTree tree = new MenuTree();
        tree.getTree(depts, node, false);
        return tree.modifyStr(tree.returnStr.toString());
    }

    @Override
    public String selectDeptMsgById(String depeId) throws Exception {
        DeptDO deptDO = this.deptDAO.selectDeptMsgById(depeId);
        return JsonUtil.writeObjectFormJSON(deptDO);
    }

    /**
     * 删除缓存
     */
    @Override
    @CacheEvict(allEntries = true, value = "monmondecakeCache")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insetDept(DeptDO deptDO) throws Exception {
        this.deptDAO.insetDept(deptDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateDeptById(DeptDO deptDO) throws Exception {
        this.deptDAO.updateDeptById(deptDO);
    }
}
